package com.xb.controller;

import com.xb.entity.*;
import com.xb.service.IIdcardService;
import com.xb.service.IPatientService;
import com.xb.service.IRegisterService;
import com.xb.service.IUserService;
import com.xb.util.IDCardUtil;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Controller
 * /api/register
 */
@RestController
@RequestMapping("/api/register")
@RequiresPermissions("system:register:*")
public class RegisterController {
    @Autowired
    private IRegisterService registerService;
    @Autowired
    private IIdcardService idcardService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private IUserService userService;

    @PostMapping("/getCardIdInfo")
    public Result getCardIdInfo(@RequestBody GetCardIdInfoReqVO getCardIdInfoReqVO) {
        Result result = new Result();
        if (getCardIdInfoReqVO.getCommand() != 1) {
            result.setStatus(Result.RESPONSE_FAIL);
        } else {
            Patient patient = patientService.checkPatient(getCardIdInfoReqVO.getCardId());
            if (patient != null) {
                result.setData(patient);
                result.setStatus(Result.RESPONSE_SUCCESS);
            } else {
                result.setMessage("没有此就诊卡信息！");
            }

        }
        return result;
    }

    @GetMapping("/getAllRegisterDoctor")
    public List<User> getAllRegisterDoctor(User user) {
        return userService.selectUserList(user);
    }

    @PostMapping("/addPatientInfo")
    public Result addPatientInfo(@RequestBody Patient patient) {
        Result result = new Result();
        try {
            boolean validateCard = IDCardUtil.validateCard(patient.getIdCard());
            if (!validateCard) {
                result.setStatus(Result.RESPONSE_FAIL);
                result.setMessage("EXISTS");
            } else {
                patient.setAge(Integer.valueOf(IDCardUtil.getAge(patient.getIdCard())));
                patient.setBirthday(IDCardUtil.getBirthday(patient.getIdCard()));
                patient.setSex(IDCardUtil.getSex(patient.getIdCard()));
                if (patientService.insertPatient(patient)) {
                    result.setData(patient.getCardId());
                    result.setStatus(Result.RESPONSE_SUCCESS);
                } else {
                    Patient idCard = patientService.selectPatientByIdCard(patient.getIdCard());
                    if (idCard != null) {
                        result.setData("卡号为：" + idCard.getCardId());
                        result.setStatus(Result.RESPONSE_FAIL);
                        result.setMessage("ACTIVATED");
                    } else {
                        result.setStatus(Result.RESPONSE_FAIL);
                        result.setMessage("COVER");
                    }
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("FAIL");
            result.setData("联系管理员 <a href=\"mailto:xb.yang1@outlook.com\">点击这里发送邮件给xb.yang1@outlook.com</a>");
            return result;
        }

    }

    @PostMapping("/coverCardId")
    public Result coverCardId(@RequestBody Patient patient) {
        Result result = new Result();
        Idcard idcard = idcardService.selectIdcardByIdCard(patient.getIdCard());
        patient.setCardId(String.valueOf(idcard.getId()));
        patient.setAge(Integer.valueOf(IDCardUtil.getAge(patient.getIdCard())));
        patient.setBirthday(IDCardUtil.getBirthday(patient.getIdCard()));
        patient.setSex(IDCardUtil.getSex(patient.getIdCard()));
        if (patientService.reissuePatient(patient)) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setData(idcard.getId());
        }
        return result;
    }

    @PostMapping("/addRegisterInfo")
    public Result addRegisterInfo(@RequestBody Register register, HttpSession session) {
        return registerService.insertRegister(register, session);
    }

    /**
     * 条件+分页查询
     * 如果是layui分页查询，pageNum 替换成 page  ， pageSize 替换成 limit
     * 如果是bootstrap分页，则page 替换成 pageNum， limit 替换成 pageSize
     */
    @GetMapping("/page")
    @RequiresPermissions("system:register:page")
    public Object findPage(Register obj, @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer limit) {
        return registerService.selectRegisterList(obj, page, limit);
    }

}
